package cn.hefan.web.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import cn.hefan.service.Service;

public class fbtlServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		String[] str = new String[2];
		String username = null ;
		String title = null;
		int i = 0;
		String fileName = null;
		//判断请求是否为multipart请求
		if(!ServletFileUpload.isMultipartContent(request)) {
			throw new RuntimeException("当前请求不支持文件上传");
		}try {
			//创建一个工
			DiskFileItemFactory factory = new DiskFileItemFactory();
			//创建一个新的文件上传处理程序
			ServletFileUpload upload = new ServletFileUpload(factory);
			//解析请求
			List <FileItem> items= upload.parseRequest(request);
			
			//遍历items
			for(FileItem item : items ) {
				if(item.isFormField()) {//如果item为普通表单项
					//获取表单项名称
					str[i] = item.getString();
					i++;
					
			
					}else {                  //如果item为文件表单项
						//获取上传文件名
						String allFilePath = item.getName();
		                int ind = allFilePath.lastIndexOf("\\");
		                if (ind != -1) {
		                    fileName = allFilePath.substring(ind + 1);
		                }else {
		                    fileName = allFilePath;
		                }
		                
						//获取输入流，其中有上传文件的内容
						InputStream is = item.getInputStream();
						//获取文件保存在服务器的路径
						String path = this.getServletContext().getRealPath("/taolun");
						//创建 目标文件，用于保存上传文件
						File descFile = new File(path, fileName);
						//创建文件输出流
						OutputStream os = new FileOutputStream(descFile);
						//将输入流的数据写入输出流
						int len = -1;
						byte[] buf = new byte[1024];
						while((len = is.read(buf))!=-1) {
							
							os.write(buf,0,len);
						}
						//关闭流
						os.close();
						is.close();
					
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
		for(int j=0;j<2;j++) {
			str[j] = new String(str[j].getBytes("ISO-8859-1"), "UTF-8");
		}
		fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
		fileName = "http://192.168.0.100:8080/UniApp/taolun/"+fileName;
		System.out.println(fileName);
		username = str[0];
		title = str[1];
		Service.addTaolun(username,title,fileName);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

}